'use strict';

const Controller = require('egg').Controller;
const exec = require('child_process').exec;

class HomeController extends Controller {
    async check() {
        // this.ctx.body = await this.ctx.service.runLighthouse.run({
        //     curUrl: 'https://cn.bing.com/?toHttps=1&redig=71FE67C1B8814FA3BD8BD8BAB5798A25',
        //     // curUrl: 'https://www.baidu.com',
        //     id: 'bbbbddddd',
        // });
        this.ctx.body='ok';
    }

    async index() {
        const { ctx } = this;
        await ctx.render('index.nj');
    }

    async run() {
        const { curUrl, id } = this.ctx.request.body;
        if (!curUrl) {
            this.ctx.status = 400;
            return;
        }
        // 获取一个唯一id生成lighthouse文件名，防止文件重复
        const { status, data, code } = await this.ctx.service.runLighthouse.run({ curUrl, id });
        this.ctx.status = status;
        this.ctx.body = { data, code };
    }
}

module.exports = HomeController;
